<?php
namespace Qgs\Model;
use \Think\Db;
use Think\Model;

class CartsModel extends Model{
    /*加入购物车*/
    //protected $tableName = 'b2c_carts';
    public function addCart(){
            $local_id = 1;
            $member_id = 714;
            $isCheck = 1;
            $pid = (int) I('post.pid');
            $goods_id = (int) I('post.goods_id');
            $cartNum = I('post.buyNum');


        //当该用户在购物车，是否有该商品
        $goods = M('b2c_carts')->where(array('member_id'=>$member_id,'product_id'=>$pid,'local_id'=>$local_id))->select();

        if(empty($goods)){
            $goodsarr = array();
            //加入购物车表
            $goodsarr['local_id'] = $local_id;
            $goodsarr['member_id'] = $member_id;
            $goodsarr['ischeck'] = $isCheck;
            $goodsarr['product_id'] = $pid;
            $goodsarr['cart_num'] = $cartNum;
            $goodsarr['goods_id'] = $goods_id;
            $goodsarr['time'] = time();
            print_r($goodsarr);
            $rs = M('b2c_carts')->add($goodsarr);
            if(false !==$rs){


                return array('msg'=>'添加成功','status'=>1);
            }
        }else{
            //根据条件更新 num
             $rs =M('b2c_carts')->where(array('member_id'=>$member_id,'product_id'=>$pid,'local_id'=>$local_id))->setInc('cart_num',$cartNum);
            return array('msg'=>'添加成功','status'=>1);
        }
        return array('msg'=>'添加失败','status'=>-1);

    }


}